﻿using CCWin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TelephoneYiShou;

namespace gongyingshang
{
    public partial class readSupplier : Skin_Mac
    {
        public readSupplier()
        {
            InitializeComponent();
        }
        public DataTable dt;
        int i, start;//i表示总行数，start表示当前位置
        int size = 10;//size表示一页显示的数量
        string str1 = "select * from Supplier";
        string str = "server=.;database = TelephoneYiShou;uid = sa;pwd = 123456";
        public void show(int i, int j)
        {
            SqlConnection sqlcon = new SqlConnection(str);
            sqlcon.Open();
            SqlCommand sqlcmd = new SqlCommand(str1, sqlcon);
            SqlDataAdapter sqlada = new SqlDataAdapter(sqlcmd);
            DataSet ds = new DataSet();
            sqlada.Fill(ds, i, j, "[Supplier]");//把数据库表中的i-j的记录数用来填充数据集
            dataGridView1.DataSource = ds.Tables["[Supplier]"];
            ds = null;//清空数据集
        }
        private void readSupplier_Load(object sender, EventArgs e)
        {
            dt = DBHelper.GetDataTable(str1);
            i = dt.Rows.Count;//获取数据的条数
            show(0, size);
            plb.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
            //供应商名称下拉框绑定     
            string sqltp = "select SupplierName from Supplier";
            DataTable dte = DBHelper.GetDataTable(sqltp);
            for (int i = 0; i < dte.Rows.Count; i++)
            {
                supnmcmb.Items.Add(dte.Rows[i][0]);
            }
        }

        private void exitbtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }



        private void readbt_Click_1(object sender, EventArgs e)
        {
            start = 0;//初始化页数
            string supnm = supnmcmb.Text;
            string sql = string.Format("select * from Supplier where SupplierName='{0}'", supnm);
            DataTable dt = DBHelper.GetDataTable(sql);
            i = dt.Rows.Count;//获取数据的条数
            if (i == 0)
            {
                MessageBoxEx.Show("没有供应商" + supnm);
            }
            else
            {
                show(0, size);
                plb.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
            }
            dataGridView1.DataSource = dt;
        }


        private void backbt_Click(object sender, EventArgs e)
        {
            if (start < 1)
            {
                MessageBoxEx.Show("已是第一页......");
            }
            else
            {
                start = start - 1;
                show(start * size, size);
            }
            plb.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
        }

        private void nextbt_Click(object sender, EventArgs e)
        {
            if (start + 1 > i / size)
            {
                MessageBoxEx.Show("已是最后页");
            }
            else
            {
                start = start + 1;
                show(start * size, size);
            }
            plb.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
        }



        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void udtbt_Click_1(object sender, EventArgs e)
        {

            dt = DBHelper.GetDataTable(str1);
            i = dt.Rows.Count;//获取数据的条数
            show(0, size);
            plb.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
            this.supnmcmb.Text = "";
        }
    }
}
